package db;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Gabrielle DuBois
 */
public class DB {
    protected final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    protected final String dbName = "StPatrick";
    protected final String dbPath = new java.io.File(".").getCanonicalPath() + "/database/";
    protected final String dbAttrValue = "";   //";create=true"
    protected final String connURL = "jdbc:derby:" + dbPath + dbName + dbAttrValue;
    protected Connection conn;

    /**
     * Constructor
     * @throws Exception, specifically a ClassNotFoundException or a
     *          SQLExcpetion to alert user that the database was not found
     */
    protected DB() throws Exception{
        //System.out.println("Path: " + dbPath);
        open();
    }//DB()


    /**
     * Opens the connection to the database
     * @throws Exception, specifically a ClassNotFoundException or a
     *          SQLExcpetion
     */
    private void open() throws Exception{
        Class.forName(driver);                          // register the driver
        //System.out.println("Attempting connection to: " + connURL);
        conn = DriverManager.getConnection(connURL);    // create a database connection
        //System.out.println("Connection successful");
        conn.setAutoCommit(false);

    }//open()

    /**
     * Closes the connection to the database
     */
    public void close(){
	try{
            conn.close(); //close the connection
        }catch (SQLException e){
            System.err.println("Error closing '" + connURL + "': " + e);
        }
    }//close()
}//DB
